<?php
namespace Crontab\Controller;

use think\Controller;

//计算货品的统计
class ProductController extends Controller{

    protected $product_statistics_model;
    protected $admin_log_model;
    public function _initialize(){
        $this->product_statistics_model = D("product_statistics");
        $this->admin_log_model = D("admin_log");
    }

    public function sent(){
        //获取昨天开始时间戳及日期
        $stime = strtotime('yesterday'); //昨天0点
        $etime = date("Y-m-d",strtotime("-1 day")); //昨天0点

//        $stime = 1591718400;
//        $etime = date("Y-m-d",$stime);
        //查询时间记录是否存在   不存在进行跟oms交互  获取当时的数据
        $info = $this->product_statistics_model->where(array("create_time"=>$stime))->find();
        if(empty($info)){
            //拼接oms  参数

            $omsUrl = C("TEST_OMS_URL").'/api/DataReport/GetAllDataReport';
            $dataArr = array(
                'ClientName' => C("TEST_OMS_CLIENT_NAME"),
                'ClientGuid' => C("TEST_OMS_CLIENT_GUID"),
                'QueryDate'  => $etime
            );
            $message = '请求oms接口地址：'.$omsUrl.'; 请求参数：'.json_encode($dataArr);
            $res = sync_data($omsUrl,$dataArr);

            $message .= '；请求结果：'.json_encode($res);
            writeLog($message,'ProductStatisticsLog');
            $msg = '请求失败';
            if(!$res || $res['code'] != '200'){
                $msg =  $res['msg'];
            }else{
                if ($res['success'] != true) {
                    $msg =  $res['msg'];
                }else{
                    $msg = '请求成功';
                    $newArr = array(
                        'spu_count'                => $res['data']['SpuCount'],
                        'sku_count'                => $res['data']['SkuCount'],
                        'self_stock_sum'           => $res['data']['SelfStockSum'],
                        'agent_stock_sum'          => $res['data']['AgentStockSum'],
                        'self_stock_price_total'   => $res['data']['SelfStockPriceTotal'],
                        'agent_stock_price_total'  => $res['data']['AgentStockPriceTotal'],
                        'vendor_count'             => $res['data']['VendorCount'],
                        'avg_price'                => $res['data']['AvgPrice'],
                        'create_time'              => $stime,
                    );
                    $addData = $this->product_statistics_model->add($newArr);

                }
            }
            $adminData = array(
                'admin_id'   => 2,
                'admin_name' => 'crontab_product',
                'method'     => 'post',
                'ip'         => get_client_ip(),
                'input'      => json_encode($dataArr),
                'created_at' => $etime,
                'path'       => 'appserver/index.php?m=Crontab&c=Product&a=sent'
            );
            $this->admin_log_model->add($adminData);
        }
    }


}